<?php

namespace MLM\Controller;

use Zend\View\Model\ViewModel;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;

class ProductController extends \MLM\Model\Generic\Controller
{
        
    public function indexAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        
        return new ViewModel();

    }
    
    public function addAction() {
        $identity = $this->hasSession();
        
        $this->layout('layout/admin');
        $view = new ViewModel();
        

        $getParams = $this->getRequest()->getQuery();
        $db = $this->_db;

        $conn = $db->getDriver()->getConnection();

        $storage = new \MLM\Model\Product\Storage($db);
        $product = new \MLM\Model\Product($storage);

        $parents = $storage->listProduct();
        $owners = $storage->listProductOwner();
        $supliers = $storage->listSuplier();

        $view->setVariable('parents', $parents);
        $view->setVariable('owners', $owners);
        $view->setVariable('supliers', $supliers);
        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            });
        })
        ");

        $request = $this->getRequest();

        if ($request->isPost()) {
            $conn->beginTransaction();
            $post = $request->getPost();

            // assign data product
            $product->productCode = $post->product_code;
            $product->productName = $post->product_name;
            $product->suplierId = $post->suplier_id;
            $product->productDesc = $post->product_desc;
            
            if ($post->parent_product_id != "") $product->parentProductId = $post->parent_product_id;
            
            $product->productOwnerId = $post->product_owner_id;

            $productId = $product->save();

            if(!$productId){
                $conn->rollback();
                $view->setVariable('errmsg', 'Penambahan Produk Gagal');
                return $view;
            }
            $product->id = $productId;


            /*
             * Product Price
             *
            $priceStorage = new \MLM\Model\Product\Price\Storage($db);
            $price = new \MLM\Model\Product\Price($priceStorage);
    
            $price->productId = $productId;
            $price->basicPrice = $post->basic_price;
            $price->salePrice = $post->sale_price;
            $price->startDat = $post->start_dat;
            $price->endDat = $post->end_dat;
  
            $priceId = $price->save();

            if(!$priceId){
                $conn->rollback();
                $view->setVariable('errmsg', 'Pembuatan harga Gagal');
                return $view;
            }


             *
             * Product Stock
             *
            $stockStorage = new \MLM\Model\Product\Stock\Storage($db);
            $stock = new \MLM\Model\Product\Stock($stockStorage);

            $stock->productId = $productId;
            $stock->qty = $post->qty;
            
            $stockId = $stock->save();

            if(!$stockId){
                $conn->rollback();
                $view->setVariable('errmsg', 'Pembuatan Stok gagal');
                return $view;
            }
             * 
             */

            
            $conn->commit();


            return $this->redirect()->toUrl('add?msg=Penambahan produk berhasil');

        }

        
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";
        $view->setVariable('msg', $msg);

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
    }


    public function editAction() {
        $identity = $this->hasSession();

        $this->layout('layout/admin');
        $view = new ViewModel();


        $getParams = $this->getRequest()->getQuery();
        

        $db = $this->_db;

        $conn = $db->getDriver()->getConnection();

        $storage = new \MLM\Model\Product\Storage($db);
        
        $parents = $storage->listProduct();
        $owners = $storage->listProductOwner();
        $supliers = $storage->listSuplier();

        $view->setVariable('parents', $parents);
        $view->setVariable('owners', $owners);
        $view->setVariable('supliers', $supliers);
        
        $request = $this->getRequest();

        if ($request->isPost()) {


            $conn->beginTransaction();
            $post = $request->getPost();
            $prodId = $post["prodId"];
            $priceId = $post["product_price_id"];
            $stockId = $post["product_stock_id"];


            // assign data product
            if ($post->parent_product_id == ""){
                $dataProduct = array("product_code" => $post->product_code,
                                      "product_name" => $post->product_name,
                                      "suplier_id" => $post->suplier_id,
                                      "product_desc" => $post->product_desc,
                                      "parent_product_id" => null,
                                      "product_owner_id" => $post->product_owner_id
                                  );

            }else{
                $dataProduct = array("product_code" => $post->product_code,
                                      "product_name" => $post->product_name,
                                      "suplier_id" => $post->suplier_id,
                                      "product_desc" => $post->product_desc,
                                      "parent_product_id" => $post->parent_product_id,
                                      "product_owner_id" => $post->product_owner_id
                                  );
            }

            $where = array("product_id" => $prodId);

            $productId = $storage->edit($dataProduct, $where);

            if(!$productId){
                $conn->rollback();
                $view->setVariable('errmsg', 'Penambahan Produk Gagal');
                return $view;
            }
            

            /*
             * Product Price
             *
            $priceStorage = new \MLM\Model\Product\Price\Storage($db);

            
            $dataPrice = array(
                    "basic_price" => $post->basic_price,
                    "sale_price" => $post->sale_price,
                    "start_dat" => $post->start_dat,
                    "end_dat" => $post->end_dat,
            );
           

            $where = array("product_price_id" => $priceId);

            $priceId = $priceStorage->edit($dataPrice, $where);

            if(!$priceId){
                $conn->rollback();
                $view->setVariable('errmsg', 'Pembuatan harga Gagal');
                return $view;
            }


            *
             * Product Stock
             *
            $stockStorage = new \MLM\Model\Product\Stock\Storage($db);
            
            
            $dataStock = array(
                    "qty" => $post->qty
            );

            $where = array("product_stock_id" => $stockId);

            $stockId = $stockStorage->edit($dataStock, $where);

            if(!$stockId){
                $conn->rollback();
                $view->setVariable('errmsg', 'Pembuatan Stok gagal');
                return $view;
            }
             *
             */


            $conn->commit();


            return $this->redirect()->toUrl('edit?msg=Edit produk berhasil');

        }

        $prodId = $getParams["prodId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();
        $produDetail = $storage->getProductDetail($prodId);
        while($produDetail->next()){
                $res = $produDetail->current();
                $data["product_code"] = $res['product_code'];
                $data["product_name"] = $res['product_name'];
                $data["suplier_id"] = $res['suplier_id'];
                $data["product_desc"] = $res['product_desc'];
                $data["parent_product_id"] = $res['parent_product_id'];
                $data["product_owner_id"] = $res['product_owner_id'];
                
        }

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            });            
        })
        ");

        $view->setVariable('msg', $msg);
        $view->setVariable('prodId', $prodId);
        $view->setVariable('data', $data);

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
    }


    public function priceAction() {
        $identity = $this->hasSession();

        $this->layout('layout/admin');
        $view = new ViewModel();


        $getParams = $this->getRequest()->getQuery();


        $storage = new \MLM\Model\Product\Storage($this->_db);

        $parents = $storage->listProduct();
        $owners = $storage->listProductOwner();
        $supliers = $storage->listSuplier();

        $view->setVariable('parents', $parents);
        $view->setVariable('owners', $owners);
        $view->setVariable('supliers', $supliers);

        $prodId = $getParams["prodId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();
        $produDetail = $storage->getProductDetail($prodId);
        while($produDetail->next()){
                $res = $produDetail->current();
                $data["product_code"] = $res['product_code'];
                $data["product_name"] = $res['product_name'];
                $data["suplier_id"] = $res['suplier_id'];
                $data["suplier_name"] = $res['suplier_name'];
                $data["suplier_address"] = $res['suplier_address'];
                $data["product_desc"] = $res['product_desc'];
                $data["parent_product_id"] = $res['parent_product_id'];

                if ($res['parent_product_id'] != ""){

                    $prodParent = $storage->getProductParent($res['parent_product_id']);
                    while($prodParent->next()){
                        $resP = $prodParent->current();
                        $data["parent_product_name"] = $resP['product_name'];
                    }

                }

                $data["product_owner_id"] = $res['product_owner_id'];
                $data["product_owner_name"] = $res['product_owner_name'];

                
        }


        $view->setVariable('msg', $msg);
        $view->setVariable('prodId', $prodId);
        $view->setVariable('data', $data);
        $view->setVariable('msg', $msg);

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
    }


    public function stockAction() {
        $identity = $this->hasSession();

        $this->layout('layout/admin');
        $view = new ViewModel();


        $getParams = $this->getRequest()->getQuery();

        
        $storage = new \MLM\Model\Product\Storage($this->_db);
        /*
        $parents = $storage->listProduct();
        $owners = $storage->listProductOwner();
        $supliers = $storage->listSuplier();

        $view->setVariable('parents', $parents);
        $view->setVariable('owners', $owners);
        $view->setVariable('supliers', $supliers);
         * 
         */

        $prodId = $getParams["prodId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();
        $produDetail = $storage->getProductDetail($prodId);
        while($produDetail->next()){
                $res = $produDetail->current();
                $data["product_code"] = $res['product_code'];
                $data["product_name"] = $res['product_name'];
                $data["suplier_id"] = $res['suplier_id'];
                $data["suplier_name"] = $res['suplier_name'];
                $data["suplier_address"] = $res['suplier_address'];
                $data["product_desc"] = $res['product_desc'];
                $data["parent_product_id"] = $res['parent_product_id'];

                if ($res['parent_product_id'] != ""){

                    $prodParent = $storage->getProductParent($res['parent_product_id']);
                    while($prodParent->next()){
                        $resP = $prodParent->current();
                        $data["parent_product_name"] = $resP['product_name'];
                    }

                }

                $data["product_owner_id"] = $res['product_owner_id'];
                $data["product_owner_name"] = $res['product_owner_name'];

        }

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
        $script->appendScript("
          jQuery(function($) {
            $('.chosen-select').chosen();

            $('#modal-form').on('shown.bs.modal', function() {
                $(this).find('.chosen-container').each(function() {
                    $(this).find('a:first-child').css('width', '210px');
                    $(this).find('.chosen-drop').css('width', '210px');
                    $(this).find('.chosen-search input').css('width', '200px');
                });
            });

            $('#qty').ace_spinner({value:10,max:999999,step:10, btn_up_class:'btn-info' , btn_down_class:'btn-info'})
                .on('change', function(){
                        //alert(this.value)
                });
        })
        ");


        //$agentStorage = new \MLM\Model\Agent\Storage($this->_db);
        //$agentLists = $agentStorage->getAgentList("", 1);

        $view->setVariable('msg', $msg);
        $view->setVariable('prodId', $prodId);
        $view->setVariable('data', $data);
        //$view->setVariable('agents', $agentLists);

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
    }


    public function productlistAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        
        $view = new ViewModel();

        $script = $this->getServiceLocator()->get('viewhelpermanager')
                ->get('inlineScript');
                $script->appendScript("
                  jQuery(function($) {
                    $('.chosen-select').chosen();

                    $('#modal-form').on('shown.bs.modal', function() {
                        $(this).find('.chosen-container').each(function() {
                            $(this).find('a:first-child').css('width', '210px');
                            $(this).find('.chosen-drop').css('width', '210px');
                            $(this).find('.chosen-search input').css('width', '200px');
                        });
                    });

                })
                ");

        $storage = new \MLM\Model\Product\Storage($this->_db);
        $supliers = $storage->listSuplier();
        $view->setVariable('supliers', $supliers);

        return $view;

    }


    public function approvewithdrawAction(){

        $identity = $this->hasSession();
        $this->layout('layout/admin');
        $view = new ViewModel();

        $request = $this->getRequest();

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        $storage = new \MLM\Model\Transaction\Storage($db);

        if ($request->isPost()) {
            $conn->beginTransaction();
            $post = $request->getPost();

            $transId   = $post['transId'];
            $flag = $post['flag'];


            $dataW = $storage->approveWithdraw($transId, $flag);
            while($dataW->next()){
                    $res = $dataW->current();
                    $msg = $res["out_msg"];
                    $code = $res['out_code'];
            }

            if ($code == '0') {
                $conn->commit();
                return $this->redirect()->toUrl('approvewithdraw?msg='.$flag.' Penarikan Sukses');

            } else {
                $conn->rollback();
                $view->setVariable('errmsg', $msg);
            }

            //return $this->redirect()->toUrl('approvewithdraw?msg='.$flag.' Penarikan Sukses');
            //die();
            
        }

        $getParams = $this->getRequest()->getQuery();
        $transId = $getParams["transId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();

        $dataTrans = $storage->getTransDetail($transId);
        while($dataTrans->next()){
            
                $res = $dataTrans->current();
                $data['account_num1'] = $res['account_num1'];
                $data['amount1'] = number_format($res['amount1']);
                $data['bank'] = "[".$res['bank_acc_no']."] ".$res['bank_name']." ".$res['bank_acc_name'];
                
        }

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        $view->setVariable('data', $data);
        $view->setVariable('transId', $transId);
        $view->setVariable('msg', $msg);

        return $view;
        
    }


    public function topupAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return new ViewModel();


    }


    public function buysnAction()
    {

        $identity = $this->hasSession();
        $this->layout('layout/admin');

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return new ViewModel();


    }

    public function buyproductAction()
    {
        $identity = $this->hasSession();
        $this->layout('layout/admin');
        $view = new ViewModel();

        $memberId = $identity->member_id;

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $memberId);

        $request = $this->getRequest();

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        if ($request->isPost()) {
            $conn->beginTransaction();
            $formData = $request->getPost();

            $sn   = $formData['sn'];
            $snList   = $formData['snList'];
            $flag   = $formData['flag'];
            
            $err = 0;
            $errorMsg = "";

            /*
             * Check List
             */
            if ($snList == "" && $flag == 1){
                $errorMsg .= "* SN harus dipilih.<br>";
                //return;
                $err = 1;
            }


            $msg = "";
            if ($err == 0){

                    if ($flag == 1){ //Aktifkan yang dipilih
                        $msg = "Diaktifkan";
                    }else if ($flag == 2){ //Aktifkan semua
                        $msg = "Diaktifkan Semua";
                    }else if ($flag == 3){  //Pasifkan semua
                        $msg = "Dipasifkan Semua";
                    }

                    $storage = new \MLM\Model\Sn\Storage($db);
                    $res = $storage->aktivasiSN($snList, $flag, $sn);

                    if(!$res){
                        $conn->rollback();
                        $view->setVariable('msg', 'SN Gagal '.$msg);
                        return $view;
                    }

                    $conn->commit();
                    $view->setVariable('msg', 'SN Berhasil '.$msg);
                    return $view;

            }else{
              
                $view->setVariable('errmsg', $errorMsg);
                return $view;
            }



        }else{
            
            return $view;
        }

       
    }


    public function confirmlistAction(){
        $identity = $this->hasSession();
        if ($identity->member_type_id == 4)
            $this->layout('layout/admin');
        else
            $this->layout('layout/member');
            
        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        
        return $view;
   }

    public function payconfirmAction(){
        $identity = $this->hasSession();
        $this->layout('layout/admin');
        $view = new ViewModel();

        $request = $this->getRequest();
        $memberId = $identity->member_id;

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        $storage = new \MLM\Model\Transaction\Storage($db);
        
        if ($request->isPost()) {
            $conn->beginTransaction();
            $post = $request->getPost();
            $confirmId = $post["confirmId"];

            $data = array(
                'payment_status'  => 1
            );
            $where = array('payment_confirm_id' => $confirmId);
            $res = $storage->updateConfirmPaymentStatus($data, $where);

            $conn->commit();

            return $this->redirect()->toUrl('payconfirm?msg=Verifikasi Pembayaran Sukses');


            die();
        }
        
        $getParams = $this->getRequest()->getQuery();
        $confirmId = $getParams["confirmId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();

        $dataConfirm = $storage->getConfirmPaymentDetail($confirmId);
        while($dataConfirm->next()){
                $res = $dataConfirm->current();
                $data['trans_code'] = $res['trans_code'];
                $data['amount1'] = number_format($res['amount1']);
                $data['pay_date'] = $res['pay_date2'];

                if ($res['pay_method'] == 1) $data['pay_method'] = "Transfer ATM";
                else if ($res['pay_method'] == 2) $data['pay_method'] = "Internet Banking";
                else if ($res['pay_method'] == 3) $data['pay_method'] = "Mobile Banking";
                else $data['pay_method'] = "Setoran/Transfer Tunai";

                $data['from_account'] = "[".$res['from_acc_no']."] ".$res['from_bank']." ".$res['from_acc_name'];
                $data['to_account'] = "[".$res['to_acc_no']."] ".$res['to_bank']." ".$res['to_acc_name'];
                $data['transfer_amount'] = number_format($res['transfer_amount']);
                $data['notes'] = $res['notes'];
        }

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $memberId);
        
        $view->setVariable('data', $data);
        $view->setVariable('confirmId', $confirmId);
        $view->setVariable('msg', $msg);

        return $view;
   }


   public function buysnlistAction(){
        $identity = $this->hasSession();
        $this->layout('layout/admin');
        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
   }


   public function approvalbuysnAction(){

        $identity = $this->hasSession();
        $this->layout('layout/admin');
        $view = new ViewModel();

        $request = $this->getRequest();
        $memberId = $identity->member_id;

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        $storage = new \MLM\Model\Transaction\Storage($db);

        if ($request->isPost()) {
            $conn->beginTransaction();
            $post = $request->getPost();

            $transId   = $post['transId'];
            $flag = $post['flag'];


            $dataA = $storage->approveBuySN($transId, $flag);
            while($dataA->next()){
                    $res = $dataA->current();
                    $msg = $res["out_msg"];
                    $code = $res['out_code'];
            }

            if ($code == '0') {
                $conn->commit();
                return $this->redirect()->toUrl('approvalbuysn?msg='.$flag.' Pembelian SN Sukses');

            } else {
                $conn->rollback();
                $view->setVariable('errmsg', $msg);
            }

            //return $this->redirect()->toUrl('approvalbuysn?msg='.$flag.' Pembelian SN Sukses');
            //die();

        }

        $getParams = $this->getRequest()->getQuery();
        $transId = $getParams["transId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();

        $dataTrans = $storage->getTransDetail($transId);
        while($dataTrans->next()){

                $res = $dataTrans->current();
                $data['trans_code'] = $res['trans_code'];
                $data['trans_dtm'] = $res['trans_dtm'];
                $data['account_num1'] = $res['account_num1'];
                $data['jumlah'] = number_format($res['xn2']);
                $data['amount1'] = number_format($res['amount1']);

                if ($res['xs3'] == 1) $data['carabayar'] = "Cash";
                else if ($res['xs3'] == 2) $data['carabayar'] = "Tukar Point";
                else $data['carabayar'] = "Transfer";
                
        }

        //Get Messages Notif
        $this->getMsgNotif($memberId);

        $view->setVariable('data', $data);
        $view->setVariable('transId', $transId);
        $view->setVariable('msg', $msg);

        return $view;

    }


    public function buyproductlistAction(){
        $identity = $this->hasSession();
        if ($identity->member_type_id == 4)
            $this->layout('layout/admin');
        else
            $this->layout('layout/member');
            
        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
   }


   public function approvalbuyproductAction(){

        $identity = $this->hasSession();
        if ($identity->member_type_id == 4)
            $this->layout('layout/admin');
        else
            $this->layout('layout/member');
            
        $view = new ViewModel();

        $request = $this->getRequest();

        $memberId = $identity->member_id;

        $db = $this->_db;
        $conn = $db->getDriver()->getConnection();

        $storage = new \MLM\Model\Transaction\Storage($db);

        if ($request->isPost()) {
            $conn->beginTransaction();
            $post = $request->getPost();

            $transId   = $post['transId'];
            $flag = $post['flag'];


            $dataA = $storage->approveBuyProduct($transId, $flag);
            while($dataA->next()){
                    $res = $dataA->current();
                    $msg = $res["out_msg"];
                    $code = $res['out_code'];
            }

            if ($code == '0') {
                $conn->commit();
                return $this->redirect()->toUrl('approvalbuyproduct?msg='.$flag.' Pembelian Produk Sukses');

            } else {
                $conn->rollback();
                $view->setVariable('errmsg', $msg);
            }

            //return $this->redirect()->toUrl('approvalbuysn?msg='.$flag.' Pembelian SN Sukses');
            //die();

        }

        $getParams = $this->getRequest()->getQuery();
        $transId = $getParams["transId"];
        $msg = (isset($getParams["msg"])) ? $getParams["msg"] : "";

        $data = array();

        $dataTrans = $storage->getTransDetail($transId);
        while($dataTrans->next()){

                $res = $dataTrans->current();
                $data['trans_code'] = $res['trans_code'];
                $data['trans_dtm'] = $res['trans_dtm'];
                $data['account_num1'] = $res['account_num1'];
                $data['jumlah'] = number_format($res['xn2']);
                $data['amount1'] = number_format($res['amount1']);
                $data['produk'] = $res['xs2'];

                $arrSup = explode("-", $res['xs1']);
                $data['suplier'] = $arrSup[1];

                if ($res['xs3'] == 1) $data['carabayar'] = "Cash";
                else if ($res['xs3'] == 2) $data['carabayar'] = "Tukar Point";
                else $data['carabayar'] = "Transfer";

        }

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $memberId);

        $view->setVariable('data', $data);
        $view->setVariable('transId', $transId);
        $view->setVariable('msg', $msg);

        return $view;

    }


    public function pricelistAction() {
        $identity = $this->hasSession();

        $this->layout('layout/member');
        $view = new ViewModel();


        if ($identity->member_id == 100 || $identity->member_reff_id == "")
            $memberId = $identity->member_id;
        else $memberId = $identity->member_reff_id;

        $view->setVariable('memberId', $memberId);
        $view->setVariable('isAgent', $identity->member_reff_id);

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);

        return $view;
    }


   //Get Messages Notif
    private function getMsgNotif($memberTypeId, $memberId){


        $identity = $this->hasSession();

        /*
        if ($memberId == 100) $memberId2 = $memberId;
        else{

            //$memberId2 = ($identity->member_reff_id == "") ? $memberId : $identity->member_reff_id;
            $memberId2 = $memberId;
            $isAgent = $identity->member_reff_id;
        }
         *
         */

        $isAgent = $identity->member_reff_id;
        $memberCode = $identity->member_code;


        $notifStorage = new \MLM\Model\Notification\Storage($this->_db);
        $notifMsg = new \MLM\Model\Notification($notifStorage);
        $newMsgCnt = $notifMsg->getCountNewMsg($memberId);
        $newMsgList = $notifMsg->getNewMsgList($memberId);

        $newAlertCnt = $notifMsg->getCountAlertApproval($memberTypeId, $memberId, $isAgent, $memberCode);
        $newAlertList = $notifMsg->getAlertApproval($memberTypeId, $memberId, $isAgent, $memberCode);

        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        $control = $this->getEvent()->getRouteMatch()->getParam('controller');

        $this->layout()->menu = $control.'\\'.$action;
        $this->layout()->newMsgCnt = $newMsgCnt;
        $this->layout()->newMsgList = $newMsgList;
        $this->layout()->newAlertCnt = $newAlertCnt;
        $this->layout()->newAlertList = $newAlertList;

    }


   private function hasSession() {
        $auth = new AuthenticationService();

        //$identity = $auth->getIdentity();

        if (!$auth->hasIdentity()) {
            //return $this->redirect()->toRoute('admin', array('controller' => 'Admin', 'action' => 'login'));
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }

}

